
function Event(){
  this._events = {}
}
Event.prototype.on = function(name, fn){
  (this._events[name] || (this._events[name] = [])).push(fn)
}
Event.prototype.emit = function(name,args){
  let cbs = this._events[name]
  if (cbs) {
    for (let i = 0, l = cbs.length; i < l; i++) {
      cbs[i].apply(this, args)
    }
  }
}

Event.prototype.off = function(name, fn){

    const cbs = this._events[name]
    let i = cbs.length
    while (i--) {
      if (cbs[i] === fn) {
        cbs.splice(i, 1)
        break
      }
    }
    return this

}
Event.prototype.once = function(name, fn){
  function on () {
    this.off(name, on)
    fn.apply(this, arguments)
  }
  on.fn = fn
  this.on(name, on)
  return this
}
function callback1(){
  console.log('111')
}
function callback2(){
  console.log('222')
}
function callback3(){
  console.log('333')
}
let myEvent = new Event()
myEvent.on('click',callback1)
myEvent.on('click',callback2)
myEvent.once('click',callback3)
myEvent.emit('click')
myEvent.off('click',callback1)
myEvent.emit('click')
